import java.util.Scanner;

//给定一个有序整型数组, 实现二分查找
public class TextDemo {
    public static void main(String[] args) {
        int[] array={1,3,5,8,9,12,15,18};
        Scanner sc=new Scanner(System.in);
        int left=0;
        int right= array.length-1;

        System.out.println("请输入要查找的数字");
        int x=sc.nextInt();
        while(left<right){
            int mid=(left+right)/2;
            if(x>array[right]||x<array[left]){
                System.out.println("找不到");
                break;
            }
             if(array[mid]<x){
                 left=mid+1;
             }else{
                 right=mid-1;
             }
            if(array[mid]==x){
                System.out.println("找到了，下标为"+mid);
                break;
            }

        }

    }
}
